package com.indiv.pmy.demo;

import java.awt.Color;
import java.awt.Graphics;

import com.indiv.pmy.input.KeyValue;
import com.indiv.pmy.input.KeyboardMonitor;
import com.indiv.pmy.input.KeyboardState;
import com.indiv.pmy.shape.FillCircle;
import com.indiv.pmy.shape.Rectangle;
import com.indiv.pmy.transform.TransformFactory;
import com.indiv.pmy.world.World2d;

public class Demo extends World2d {

	Rectangle r;
	
	KeyboardMonitor km;
	KeyboardState newS;
	KeyboardState oldS;
	
	double[] translation;
	
	public static void main(String [] args) {
		Demo d = new Demo();
	}
	
	public Demo() {
		super(500,500,"Demo");
	}
	
	double[] translationMatrix;
	double[][] rotationMatrix;
	double[] scaleMatrix;
	double angle = 0;
	
	@Override
	public void update() {
		oldS = newS;
		newS = km.getKeyboardState();
		
		translationMatrix = TransformFactory.generateTranslationMatrix(0, 0);
		rotationMatrix = TransformFactory.generateRotationMatrix(angle);
		scaleMatrix = TransformFactory.generateScaleMatrix(1);
		
		/*Handle translation*/
		if(newS.isPressed(KeyValue.UP)) {
			translationMatrix[1] = -1;
		} else if(newS.isPressed(KeyValue.DOWN)) {
			translationMatrix[1] = 1;
		}
		if(newS.isPressed(KeyValue.LEFT)) {
			translationMatrix[0] = -1;
		} else if(newS.isPressed(KeyValue.RIGHT)) {
			translationMatrix[0] = 1;
		}
		
		r.translate(translationMatrix);
		
		/*Handle scaling*/
		if(newS.isPressed(KeyValue.Q)) {
			scaleMatrix[0] = scaleMatrix[1] = .95;
		} else if(newS.isPressed(KeyValue.W)) {
			scaleMatrix[0] = scaleMatrix[1] = 1.05;
		}
		
		r.scale(scaleMatrix);
		
		/*Handle rotating*/
		if(newS.isPressed(KeyValue.R)) {
			angle=3;
		} else if(newS.isPressed(KeyValue.T)) {
			angle=-3;
		}
		rotationMatrix = TransformFactory.generateRotationMatrix(angle);
		r.rotate(rotationMatrix);
		angle = 0;
	}

	@Override
	public void render(Graphics g) {
		r.draw(g);
	}

	@Override
	public void init() {
		setBackgroundColor(Color.LIGHT_GRAY);
		r = new Rectangle(100,100,50,50);
		KeyValue[] keys = {KeyValue.UP,KeyValue.DOWN,
						   KeyValue.LEFT,KeyValue.RIGHT,
						   KeyValue.R,KeyValue.T,
						   KeyValue.Q,KeyValue.W};
		km = new KeyboardMonitor(keys, this);
		newS = km.getKeyboardState();
		oldS = km.getKeyboardState();
		
		start();
	}
}
